﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;

namespace blackhouse.BlackApart.Data {
	public abstract class AdoCompanyDataAccess : ICompanyDataAccess {

		#region ICompanyDataAccess

		public CompanyCountsData GetCompanyCounts() {
			return this.ReadCounts(this.GetReaderForCompanyCounts());
		}

		public ApartmentPromotionBanerData[] GetBanners(string language) {
			return this.ReadBanners(this.GetReaderForBaners(language));
		}

		public LanguageData[] GetLanguages() {
			return this.ReadLanguages(this.GetReaderForLanguages());
		}

		#endregion

		#region Methods

		public abstract IDataReader GetReaderForCompanyCounts();
		public abstract IDataReader GetReaderForBaners(string language);
		public abstract IDataReader GetReaderForLanguages();

		private ApartmentPromotionBanerData[] ReadBanners(IDataReader reader) {
			List<ApartmentPromotionBanerData> data = new List<ApartmentPromotionBanerData>();
			while (reader.Read()) {
				data.Add(this.ReadBannerFields(reader));
			}
			reader.Close();
			return data.ToArray();
		}

		private ApartmentPromotionBanerData ReadBannerFields(IDataReader reader) {
			ApartmentPromotionBanerData data = new ApartmentPromotionBanerData();
			data.ApartmentGroupId = Convert.ToInt32(reader["ApartmentGroupId"]);
			data.Baner = Convert.ToString(reader["Baner"]);
			data.Text = Convert.ToString(reader["Text"]);
			return data;
		}

		private LanguageData[] ReadLanguages(IDataReader reader) {
			List<LanguageData> data = new List<LanguageData>();
			while (reader.Read()) {
				data.Add(this.ReadLanguageFields(reader));
			}
			reader.Close();
			return data.ToArray();
		}

		private LanguageData ReadLanguageFields(IDataReader reader) {
			LanguageData data = new LanguageData();
			data.LanguageCode = Convert.ToString(reader["LanguageCode"]);
			data.CurrencyCode = Convert.ToString(reader["CurrencyCode"]);
			return data;
		}

		private CompanyCountsData ReadCounts(IDataReader reader) {
			CompanyCountsData data = default(CompanyCountsData);
			if (reader.Read())
				data = this.ReadCountsFields(reader);
			reader.Close();
			return data;
		}

		private CompanyCountsData ReadCountsFields(IDataReader reader) {
			CompanyCountsData data = new CompanyCountsData();
			data.Clients = Convert.ToInt32(reader["Clients"]);
			data.Opinions = Convert.ToInt32(reader["Opinions"]);
			data.Photos = Convert.ToInt32(reader["Photos"]);
			return data;
		}

		#endregion

	}
}
